package com.chuanjiabao.wisdom.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.adapter.FamilyNameListAdapter;
import com.chuanjiabao.wisdom.bean.FamilyDeatilCallbackBean;
import com.chuanjiabao.wisdom.bean.ReasionInfoCallbackBean;
import com.chuanjiabao.wisdom.databinding.ActivityFamilySelectRelationBinding;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.ui.view.TaoLinear;
import com.chuanjiabao.wisdom.util.GsonUtil;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.SpResourceUtil;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class FamilySelectRelationActivity extends BaseActivity{

    ActivityFamilySelectRelationBinding binding;

    ReasionInfoCallbackBean.DataBean bean;
    String homeId;

    FamilyNameListAdapter adapter;
    List<FamilyDeatilCallbackBean.DataBean.MembersBean> names = new ArrayList<>();

    //名字
    List<String> strs = new ArrayList<>();
    List<String> strsSecond = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(
                this,
                R.layout.activity_family_select_relation
        );
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        setBack();
        setRightText("确定");
        setRightTextDraw(R.drawable.module_draw_success_ok);

        homeId = SpResourceUtil.getHomeId();
        if (homeId != null) {
            getFamilyDetail(homeId);
        }

        adapter = new FamilyNameListAdapter(this, names);
        TaoLinear taoLinear = new TaoLinear(FamilySelectRelationActivity.this, 4);
        taoLinear.setScrollEnabled(true);
        binding.recycler.setLayoutManager(taoLinear);
        binding.recycler.setAdapter(adapter);

        adapter.setOnPositionClickListener(position -> {
            if (!strs.contains(names.get(position).getD_id() + "")) {
                strs.add(names.get(position).getD_id() + "");
            } else {
                strsSecond = strs;
                strsSecond.remove(names.get(position).getD_id() + "");
                strs = strsSecond;
            }
        });

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.rightText) {
            String relation = "";
            if (strs.size() != 0) {
                for (String s : strs) {
                    //noinspection StringConcatenationInLoop
                    relation += "," + s;
                }
                relation = relation.substring(1);
            }
            Intent intent = new Intent();
            intent.putExtra("module_bean", relation);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    public void getFamilyDetail(String homeId) {
        HTTPAPI.getInstance().getFamilyDetail(homeId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(FamilySelectRelationActivity.this, s);
                } else {
                    try {
                        FamilyDeatilCallbackBean callbackBean = GsonUtil.GsonToBean(s, FamilyDeatilCallbackBean.class);
                        names.clear();
                        for (FamilyDeatilCallbackBean.DataBean dataBean : callbackBean.getData()) {
                            names.addAll(dataBean.getMembers());
                        }
                        adapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

}
